home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / crcset11.zip / VALIDCRC.C < prev    next >
C/C++ Source or Header  |  1990-11-16  |  2KB  |  88 lines

  1. /*
  2. VALIDCRC.C
  3.  
  4. Kevin Dean
  5. Fairview Mall P.O. Box 55074
  6. 1800 Sheppard Avenue East
  7. Willowdale, Ontario
  8. CANADA    M2J 5B9
  9. CompuServe ID: 76336,3114
  10.  
  11. November 16, 1990
  12.  
  13.     This module validates the CRC of the program in which it is linked.
  14. The code was designed as an anti-virus algorithm.  The CRC is a very effective
  15. method of detecting viruses; any virus that attaches itself to the program
  16. changes the CRC of the program.  The response to an invalid CRC is entirely up
  17. to the programmer.
  18.  
  19.     This code is public domain.
  20. */
  21.  
  22.  
  23. #include <dir.h>
  24. #include <dos.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27.  
  28. #include "viruscrc.h"
  29.  
  30.  
  31. /* Macros to extract low and high bytes of a word. */
  32. #define lowb(x)  (*(unsigned char *)&(x))
  33. #define hib(x)   (*((unsigned char *)&(x) + 1))
  34.  
  35. /* Macros to extract low and high words of a dword. */
  36. #define loww(x)  (*(unsigned short *)&(x))
  37. #define hiw(x)   (*((unsigned short *)&(x) + 1))
  38.  
  39.  
  40. /***/
  41. /* Calculate CRC of active program and compare it to CRC in _viruscrc. */
  42. int isvalidcrc(const char *progname)
  43. {
  44. char buffer[1024];        /* Buffer for file's data. */
  45. crc32_t table[256];        /* CRC table. */
  46. int valid;            /* Validity of CRC. */
  47. register size_t i;        /* Byte counter. */
  48. register crc32_t *halfi;    /* Pointer to CRC of i / 2. */
  49. crc32_t crc;            /* Current CRC. */
  50. FILE *progfile;            /* Program file. */
  51. char *bufptr;            /* Pointer to buffer. */
  52. char *pn;            /* Pointer to program name. */
  53.  
  54. if (_osmajor < 3)
  55.   /* Search PATH for program file. */
  56.   pn = searchpath(progname);
  57. else
  58.   /* Under DOS versions 3 and above, the program name is in _argv[0]. */
  59.   pn = _argv[0];
  60.  
  61. if (_viruscrc.x.polynomial != 0)
  62.   if ((progfile = fopen(pn, "rb")) != NULL)
  63.     {
  64.     /* Generate a CRC lookup table for faster calculation. */
  65.     for (i = 0, halfi = table, table[0] = 0; i < 256; i += 2, halfi++)
  66.       if (hib(hiw(*halfi)) & 0x80)
  67.     table[i] = (table[i + 1] = *halfi << 1) ^ _viruscrc.x.polynomial;
  68.       else
  69.     table[i + 1] = (table[i] = *halfi << 1) ^ _viruscrc.x.polynomial;
  70.  
  71.     crc = 0;
  72.     while ((i = fread(buffer, 1, sizeof(buffer), progfile)) != 0)
  73.       for (bufptr = buffer; i--; bufptr++)
  74.     crc = (crc << 8) ^ table[hib(hiw(crc)) ^ *bufptr];
  75.  
  76.     fclose(progfile);
  77.  
  78.     valid = crc == _viruscrc.x.crc;
  79.     }
  80.   else
  81.     /* If unable to open program file, assume CRC is invalid. */
  82.     valid = 0;
  83. else
  84.   /* CRC polynomial must be something other than 0. */
  85.   valid = 0;
  86.  
  87. return (valid);
  88. }